/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OrderAddPanel.java
 *
 * Created on 2-gen-2014, 16.24.39
 */
package APP3.bookshop.gui;

import APP3.bookshop.book.ABook;
import APP3.bookshop.main.BookShop;
import APP3.bookshop.order.Order;
import APP3.bookshop.person.publisher.Publisher;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author finalfire
 */
public class OrderAddPanel extends javax.swing.JPanel {

    private BookShop bookShop;

    /**
     * Creates new form OrderAddPanel
     */
    public OrderAddPanel(BookShop bookShop) {
        this.bookShop = bookShop;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        AddOrder_publisherCombo = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        AddOrder_booksList = new javax.swing.JList();
        addBookButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        AddOrder_orderTable = new javax.swing.JTable();
        saveButton = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        jLabel1.setText("Add an order");

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(51, 51, 51));
        jLabel2.setText("Select a publisher then add the books.");

        jLabel8.setForeground(new java.awt.Color(51, 51, 51));
        jLabel8.setText("Publisher");

        AddOrder_publisherCombo.setModel(bookShop.getPublisherService().populatePublisherComboBox());
        AddOrder_publisherCombo.addActionListener (new ActionListener () {
            public void actionPerformed(ActionEvent e) {
                actionPerformedComboBOxOrderPublisher();
            }
        });
        AddOrder_publisherCombo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                AddOrder_publisherComboMouseClicked(evt);
            }
        });

        jLabel9.setForeground(new java.awt.Color(51, 51, 51));
        jLabel9.setText("Books by this publisher");

        jScrollPane1.setViewportView(AddOrder_booksList);

        addBookButton.setText("Add the book");
        addBookButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBookButtonActionPerformed(evt);
            }
        });

        AddOrder_orderTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Book title", "Copies"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(AddOrder_orderTable);

        saveButton.setText("Save your order");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(24, 24, 24)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                            .add(jLabel9)
                            .add(layout.createSequentialGroup()
                                .add(jLabel8)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(AddOrder_publisherCombo, 0, 333, Short.MAX_VALUE))
                            .add(layout.createSequentialGroup()
                                .add(jLabel1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 86, Short.MAX_VALUE)
                                .add(jLabel2))))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 406, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, addBookButton)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, saveButton))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(AddOrder_publisherCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel8))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel9)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(addBookButton)
                .add(18, 18, 18)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 148, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(saveButton)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
boolean inserted = false;
    private void addBookButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBookButtonActionPerformed
        List<ABook> selectedBook = AddOrder_booksList.getSelectedValuesList();
        if (selectedBook == null || selectedBook.size() <= 0) {
            JOptionPane.showMessageDialog(null, "Selecte at least one book");
        }

        DefaultTableModel tm = new DefaultTableModel(new String[]{"Book title", "Copies"}, 0);
        if (inserted) {
            for (int i = 0; i < AddOrder_booksList.getModel().getSize(); i++) {
                if (AddOrder_booksList.getModel().getSize() > 0) {
                    ABook b = (ABook) ((DefaultTableModel) AddOrder_booksList.getModel()).getValueAt(i, 0);
                    Integer num = (Integer) ((DefaultTableModel) AddOrder_booksList.getModel()).getValueAt(i, 1);
                    tm.addRow(new Object[]{b, num});
                }
            }
        }
        for (ABook aBook : selectedBook) {
            tm.addRow(new Object[]{aBook, "1"});
        }

        inserted = true;
        AddOrder_orderTable.setModel(tm);
    }//GEN-LAST:event_addBookButtonActionPerformed

    private void AddOrder_publisherComboMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AddOrder_publisherComboMouseClicked

    }//GEN-LAST:event_AddOrder_publisherComboMouseClicked

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if (AddOrder_orderTable.getRowCount() > 0) {
            Order o = new Order();
            o.setOrderDate(new Date());//today Date
            o.setPublisher((Publisher) AddOrder_publisherCombo.getSelectedItem());

            for (int i = 0; i < AddOrder_orderTable.getRowCount(); i++) {
                ABook b = (ABook) AddOrder_orderTable.getValueAt(i, 0);
                Integer copies = Integer.valueOf((String) AddOrder_orderTable.getValueAt(i, 1));
                o.getaBookCopies().put(b, copies);
            }
        }
        JOptionPane.showMessageDialog(null, "Order SAVED");

    }//GEN-LAST:event_saveButtonActionPerformed

    public void actionPerformedComboBOxOrderPublisher() {
        Publisher p = (Publisher) AddOrder_publisherCombo.getSelectedItem();
        AddOrder_booksList.setModel(bookShop.getPublisherService().populatePublisherBooksList(p));
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList AddOrder_booksList;
    private javax.swing.JTable AddOrder_orderTable;
    private javax.swing.JComboBox AddOrder_publisherCombo;
    private javax.swing.JButton addBookButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
